package com.psa.element;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.psa.tool.DBTool;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by david on 2014-10-08.
 * BillNo,MerchandiseID,SalesType,Quantity,Price
 */
public class OrderInvoiceItem implements ISqlObject {
    public int BillNo = 0;
    public String MerchandiseID = "";
    public String SalesType = "";
    public int Quantity = 0;
    public double Price = 0.0;
    public Merchandise merchandise;
    public int Days = 3;

    @Override
    public int insert(DBTool dbTool) {
        ContentValues values = new ContentValues();
        values.put("BillNo", BillNo);
        values.put("MerchandiseID", MerchandiseID);
        values.put("SalesType", SalesType);
        values.put("Quantity", Quantity);
        values.put("Price", Price);
        return (int)dbTool.insert("OrderInvoiceItem", values);
    }

    @Override
    public int update(DBTool dbTool) {
        return 0;
    }

    @Override
    public int delete(DBTool dbTool) {
        String where = "BillNo = ?";
        String[] args = { String.valueOf(BillNo) };
        return dbTool.delete("OrderInvoiceItem", where, args);
    }

    @Override
    public ISqlObject full(Cursor cursor) {
        if (cursor!=null && cursor.moveToNext()) {
            OrderInvoiceItem orderInvoiceItem = new OrderInvoiceItem();
            orderInvoiceItem.BillNo = cursor.getInt(0);
            orderInvoiceItem.MerchandiseID = cursor.getString(1).trim();
            orderInvoiceItem.SalesType = cursor.getString(2).trim();
            orderInvoiceItem.Quantity = cursor.getInt(3);
            orderInvoiceItem.Price = cursor.getDouble(4);
            orderInvoiceItem.merchandise = Merchandise.getMerchandise(orderInvoiceItem.MerchandiseID);
            return orderInvoiceItem;
        }
        return null;
    }

    @Override
    public void clear(DBTool dbTool) {
        dbTool.execSQL("Delete From OrderInvoiceItem Where BillNo NOT IN(Select BillNo From OrderInvoice)");
        dbTool.execSQL("Delete From OrderInvoiceItem Where BillNo In(Select BillNo From OrderInvoice Where OrderDate>date('now','-"+Days+" day'))");//
    }

    @Override
    public String getDataSql() {
        return "OrderInvoiceItem"+(Days==3?"":Days);
    }

    @Override
    public String getSyncMethod() {
        return "getdataobjects";
    }
}
